package com.ht.searchrooky.client.gui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import com.ht.searchrooky.client.gui.service.ReceiveInfListener;
import com.ht.searchrooky.client.gui.service.SendMessageUtil;
import com.ht.searchrooky.domain.User;
import com.ht.searchrooky.request.Request;

public class MainFrame extends Thread{

	private JFrame frame;
	private JTable table;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	
	//===========================================
	String[] usershead=	new String[] {
			"New column", "New column", "New column", "New column", "New column", "New column"
	};
	int rowcount;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	private JTextField textField_10;
	private JTextField textField_11;
//	 TableModel model= new DefaultTableModel(
//				new Object[][] {
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//						{null, null, null, null, null, null},
//					},usershead)  ;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame window = new MainFrame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainFrame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setPreferredSize(new Dimension(700, 450));
		frame.setBounds(100, 100, 700, 450);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		frame.setVisible(true);
		JTextPane txtpnMessagepublich = new JTextPane();
		
		
		
		txtpnMessagepublich.setText("messagepublich");
		frame.getContentPane().add(txtpnMessagepublich, BorderLayout.NORTH);
		
		final JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		
		 final CardLayout cardlayout=new CardLayout();
		panel.setLayout(cardlayout);
		
		JPanel showUsersPanel = new JPanel();
		panel.add(showUsersPanel, "name_44624593046382");
		showUsersPanel.setLayout(null);
		
		JPanel addUserPanel = new JPanel();
		panel.add(addUserPanel, "name_44730097829317");
		addUserPanel.setLayout(null);
		
		JLabel lblId = new JLabel("id");
		lblId.setBounds(57, 28, 54, 15);
		addUserPanel.add(lblId);
		
		JLabel lblName = new JLabel("name");
		lblName.setBounds(57, 60, 54, 15);
		addUserPanel.add(lblName);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setBounds(57, 103, 54, 15);
		addUserPanel.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("New label");
		lblNewLabel_1.setBounds(57, 146, 54, 15);
		addUserPanel.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setBounds(57, 186, 54, 15);
		addUserPanel.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("New label");
		lblNewLabel_3.setBounds(57, 232, 54, 15);
		addUserPanel.add(lblNewLabel_3);
		
		textField = new JTextField();
		textField.setBounds(262, 25, 131, 21);
		addUserPanel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(262, 67, 131, 21);
		addUserPanel.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(262, 100, 131, 21);
		addUserPanel.add(textField_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(262, 143, 131, 21);
		addUserPanel.add(textField_3);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(262, 183, 131, 21);
		addUserPanel.add(textField_4);
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(262, 226, 131, 21);
		addUserPanel.add(textField_5);
		
		JButton btnConfirm = new JButton("confirm");
		btnConfirm.setBounds(284, 311, 93, 23);
		addUserPanel.add(btnConfirm);
		
		JButton btnCancel = new JButton("cancel");
		btnCancel.setBounds(115, 311, 93, 23);
		addUserPanel.add(btnCancel);
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, "name_6705584777555");
		panel_1.setLayout(null);
		
		JLabel lblWelcome = new JLabel("                    welcome");
		lblWelcome.setFont(new Font("宋体", Font.PLAIN, 24));
		lblWelcome.setSize(30,150);
		
		lblWelcome.setBounds(0, 0, 684, 370);
		panel_1.add(lblWelcome);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu menu = new JMenu("开始");
		menuBar.add(menu);
		
		JMenuItem mntmuser = new JMenuItem("查看全部user");
		mntmuser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cardlayout.show(panel, "name_122411832489255");
				Request req=new Request();
				req.setUser(new User());
				
			Request response=	SendMessageUtil.getSampleClient().send(req);
			List<User> list=(List<User>)response.getList();
			fillTable(list)	;
			}
		});
		menu.add(mntmuser);
		
		JMenuItem mntmuser_1 = new JMenuItem("看看user");
		mntmuser_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cardlayout.show(panel, "name_44730097829317");
			}
		});
		menu.add(mntmuser_1);
		
		JMenuItem welcome = new JMenuItem("welcome");
		welcome.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cardlayout.show(panel, "name_6705584777555");
			}
		});
		menu.add(welcome);
		
		
		cardlayout.show(panel, "name_6705584777555");
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2, "name_122411832489255");
		panel_2.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(20, 10, 452, 266);
		panel_2.add(scrollPane);
		
		table = new JTable();table.setEnabled(false);
        table.setBackground(Color.cyan);
        //Sets the foreground color of this component
        table.setForeground(Color.black);
		 table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			 public void mouseClicked(MouseEvent e) {
			     if (e.getSource() instanceof JTableHeader) {
			         if (SwingUtilities.isRightMouseButton(e)) {
			              // right mouse button click in JTableHeader
			          }
			     } else {
			         if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
			            // double left mouse button click in JTable
			            //Returns the index of the row that point lies in
			            int row = table.rowAtPoint(e.getPoint());
			            //Returns the index of the column that point lies in
			            int column = table.columnAtPoint(e.getPoint());
			            //return cell value
			            System.out.println(table.getValueAt(row, column));
			            int id=(Integer)table.getValueAt(row, 0);
			            String name=(String)table.getValueAt(row, 1);
			            textField_6.setText(id+"");
			            textField_7.setText(name);
			            
			           rowcount=row; 
			            
			            
			            
			            } else if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
			            	  //int row = table.rowAtPoint(e.getPoint());
			            	  
			            }
			        }
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
			},
			new String[] {
				"New column", "New column", "New column", "New column", "New column", "New column"
			}
		));
		scrollPane.setViewportView(table);
		
		JLabel lblNewLabel_4 = new JLabel("id");
		lblNewLabel_4.setBounds(514, 17, 54, 15);
		panel_2.add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("name");
		lblNewLabel_5.setBounds(514, 68, 54, 15);
		panel_2.add(lblNewLabel_5);
		
		textField_6 = new JTextField();
		textField_6.setBounds(608, 14, 66, 21);
		panel_2.add(textField_6);
		textField_6.setColumns(10);
		
		textField_7 = new JTextField();
		textField_7.setBounds(608, 65, 66, 21);
		panel_2.add(textField_7);
		textField_7.setColumns(10);
		
		JButton btnNewButton = new JButton("delete");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String id=textField_6.getText().trim();
				int userid=Integer.parseInt(id);
				Request req=new Request();
				User user=new User();
				user.setId(userid);
				req.setUser(user);
				  DefaultTableModel tableModel = (DefaultTableModel) table 
				  .getModel(); 
				  tableModel.removeRow(rowcount);
				  rowcount=-1;
			Request response=	SendMessageUtil.getSampleClient().send(req);
		
			System.out.println("success delete"+response.getUser());
			cleartextfield();
			}
		});
		btnNewButton.setBounds(49, 314, 93, 23);
		panel_2.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("uptate");
		btnNewButton_1.setBounds(224, 314, 93, 23);
		panel_2.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("add");
		btnNewButton_2.setBounds(381, 314, 93, 23);
		panel_2.add(btnNewButton_2);
		
		JLabel label = new JLabel("id");
		label.setBounds(514, 111, 54, 15);
		panel_2.add(label);
		
		JLabel label_1 = new JLabel("id");
		label_1.setBounds(514, 156, 54, 15);
		panel_2.add(label_1);
		
		JLabel label_2 = new JLabel("id");
		label_2.setBounds(514, 199, 54, 15);
		panel_2.add(label_2);
		
		JLabel label_3 = new JLabel("id");
		label_3.setBounds(514, 239, 54, 15);
		panel_2.add(label_3);
		
		textField_8 = new JTextField();
		textField_8.setColumns(10);
		textField_8.setBounds(608, 108, 66, 21);
		panel_2.add(textField_8);
		
		textField_9 = new JTextField();
		textField_9.setColumns(10);
		textField_9.setBounds(608, 153, 66, 21);
		panel_2.add(textField_9);
		
		textField_10 = new JTextField();
		textField_10.setColumns(10);
		textField_10.setBounds(608, 196, 66, 21);
		panel_2.add(textField_10);
		
		textField_11 = new JTextField();
		textField_11.setColumns(10);
		textField_11.setBounds(608, 236, 66, 21);
		panel_2.add(textField_11);
		
		new ReceiveInfListener(txtpnMessagepublich).start();
	
	}
	
	 public void fillTable(List<com.ht.searchrooky.domain.User> users){ 
		  DefaultTableModel tableModel = (DefaultTableModel) table 
		  .getModel(); 
		  tableModel.setRowCount(0);// 
		  
		  // 
		  for(User user:users){ 
		    Object[] arr=new Object[6]; 
		    arr[0]=user.getId();
		    arr[1]=user.getUser_name();
		    
		    
		    // 
		    tableModel.addRow(arr); 
		  } 
		  
		  // 
		  table.invalidate(); 
		} 
	 
	 
	 public void cleartextfield(){
		 textField_6.setText(null);
		 textField_7.setText(null);
		 textField_8.setText(null);
		 textField_9.setText(null);
		 textField_10.setText(null);
		 textField_11.setText(null);
		 
	 }
	public void run(){
		
		
	}
}
